//
//  SamNetworkingTool.m
//  GoodFellow
//
//  Created by Sam on 16/8/31.
//  Copyright © 2016年 AP-EC. All rights reserved.
//

#import "SamNetworkingTool.h"

@implementation SamNetworkingTool

//单例实现
+ (SamNetworkingTool *)sharedSamNetworkingTool {
    
    static SamNetworkingTool *_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
//        _instance = [[self alloc]initWithBaseURL:[NSURL URLWithString:nil]];
        //  基本路径
        _instance = [[SamNetworkingTool alloc]initWithBaseURL:[NSURL URLWithString:@"http://b2b.ap88.com/"]];
//http://b2b.ap88.com/
        //  申明请求的数据是json类型
        _instance.requestSerializer=[AFJSONRequestSerializer serializer];
        
        //  设置请求头格式！！！
//        [_instance.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [_instance.requestSerializer setValue:@"ios" forHTTPHeaderField:@"cc"];

        // 设置可接受的类型
        _instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"application/json",@"text/json",@"text/javascript",@"text/html",nil];
        
        //设置请求超时时间
         _instance.requestSerializer.timeoutInterval = 15;
        
       
        
    });
    
    [_instance setToken];
    
    return _instance;
}
#pragma mark - 设置token
-(void)setToken{
    
    UserInfo *userinfo = [UserInfo sharedUserInfo];
    [userinfo loadUserInfoFromSanbox];

    [self.requestSerializer setValue:userinfo.token forHTTPHeaderField:@"token"];
    
//    NSLog(@"%@",userinfo.token);
    
}
//      如果在没有设置基本路径的情况下：
//    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[BaseUrl stringByAppendingString:url]]];

//get请求
- (void )get:(NSString *)url parameter:(id)parameters success:(void (^)(id responseObject))success faliure:(void (^)(id error))failure
{
    [self GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        if(responseObject)  {
            
            success(responseObject);
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
    
}

//post请求
- (void)post:(NSString *)url parameters:(id)parameters success:(void(^)(id responseObject))success faliure:(void(^)(id error))failure {
    
   [self POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if(responseObject)  {
            
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
    
}


//文件上传
- (void )post:(NSString *)url parameters:(id)parameters  constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block success:(void (^)(id responseObject))success faliure:(void (^)(id error))failure{
    
    [self POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        block(formData);
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if(responseObject)  {
            
            success(responseObject);
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        failure(error);
        
    }];
    
}


//文件下载
- (void)downloadTaskWithUrl:(NSString *)url progress:(void (^)(id downloadProgress))ProgressBlock savePath:(NSString *)savePath  completionHandler:(void (^)(NSURLResponse *response ,NSURL *filePath))completion  error:(void (^)(id error))failure{
    
    //创建请求
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    
    //创建任务
    NSURLSessionDownloadTask *download =  [self downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        
        if (downloadProgress) {
            ProgressBlock(downloadProgress);
        }
        
        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        
        return  [NSURL fileURLWithPath:savePath];
        
    }completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        
        if (!error) {
            completion(response,filePath);
        }
        else {
            
            failure(error);
            
        }
        
        
    }];
    //开启任务
    [download resume];   
}
@end

